<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\CategoryModel;
use app\admin\model\CategoryTypeModel;
use tree\Tree;
use app\admin\service\CategoryService;


/**
 * Class UserController
 * @package app\admin\controller
 * @adminMenuRoot(
 *     'name'   => '标签管理',
 *     'action' => 'default',
 *     'parent' => 'admin/Service/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   => '',
 *     'remark' => '标签管理'
 * )
 */
class CategoryController extends AdminBaseController
{
    
    /**
     * 标签列表
     * @adminMenu(
     *     'name'   => '标签列表',
     *     'parent' => 'admin/Category/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $type_id = $this->request->param('type_id',0,'intval');
        if(empty($type_id)){
            $type_id = CategoryTypeModel::order('id asc')->value('id');
        }
        $this->assign('type_id',$type_id);
        
        $cateList = CategoryTypeModel::select();
        $this->assign('cateList',$cateList);
        
        $result = CategoryModel::where(['status'=>['egt',0],'type_id'=>$type_id])
        ->order('sort asc')
        ->select()
        ->toArray();
        
        $tree = new Tree();
        $tree->icon = ['│ ', '├─ ', '└─ '];
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $array = [];
        foreach ($result as $r) {
            $r['str_manage'] = '<a href="' . url("Category/add", array("pid" => $r['id'],"type_id"=>$type_id)) . '">添加子标签</a>
					 | <a href="' . url("Category/edit", array("id" => $r['id'],"type_id"=>$type_id)) . '">修改</a>
					 | <a class="js-ajax-delete" href="' . url("Category/deletealias", array("id" => $r['id'],"type_id"=>$type_id)) . ' " data-callback="removeAll" data-msg="将删除所有下级子菜单，确认删除？">删除</a>';
            $r['parentid_node'] = ($r['parent_id']) ? ' class="child-of-node-' . $r['parent_id'] . '"' : '';
            $r['status'] = $r['status'] ? 'fa-check' : 'fa-close';
            array_push($array, $r);
        }
        $tree->init($array);
        $str = "<tr id='node-\$id' \$parentid_node data-id='\$cid'>
					<td style='text-align:center;'><input type='checkbox' class='js-check' data-yid='js-check-y' data-xid='js-check-x' name='ids[]' value='\$cid'></td>
					<td>\$id</td>
					<td>\$spacer \$name</td>
					<td><input name='sort[\$id]' data-type='sort' type='text' size='3' value='\$sort' class='form-control input-sm input-order input-ajax'></td>
					<td style='text-align:center;'><i class='fa \$status' data-type='status'></i></td>
					<td>\$str_manage</td>
				</tr>";
        
        $taxonomys = $tree->getTree(0, $str);
        $this->assign("taxonomys", $taxonomys);
        
        $list = CategoryModel::with('categoryType')->where(['type_id'=>$type_id])->order('sort asc,id asc');
        $this->assign('list',$list);
        
        return $this->fetch();
    }
    
    /**
     * 标签添加
     * @adminMenu(
     *     'name'   => '标签添加',
     *     'parent' => 'admin/Category/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签添加',
     *     'param'  => ''
     * )
     */
    public function add($type_id)
    {
         $request = $this->request->param();
         // var_dump($request);
        if(request()->isPost()){
            $request = $this->request->param();
            $id = CategoryModel::where('type_id',$type_id)->max('id');
            $id++;
            $re = CategoryModel::create([
                'type_id'=>$type_id,
                'id'=>$id,
                'parent_id'=>$request['parent_id'],
                'name'=>$request['name'],
                'img'=>$request['img'],
                'status'=>$request['status'],
            ]);
            if($re){
                $this->success('修改成功');
            }
            else{
                $this->error('修改失败');
            }
        }

        $this->assign('type_id',$type_id);
        $selectCategorys = CategoryService::getCategoryOptionsByTypeId($type_id,$this->request->param('pid'));
        $this->assign('selectCategorys',$selectCategorys);
        
        return $this->fetch();
    }
    
    /**
     * 标签修改
     * @adminMenu(
     *     'name'   => '标签修改',
     *     'parent' => 'admin/Category/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签修改',
     *     'param'  => ''
     * )
     */
    public function edit($id,$type_id)
    {
        if(request()->isPost()){
            $request = $this->request->param();
            $category = CategoryModel::get(['id'=>$id,'type_id'=>$type_id]);
            $category->parent_id = $request['parent_id'];
            $category->name = $request['name'];
            $category->img = $request['img'];
            $category->status = $request['status'];
            $re = $category->save();
            if($re){
                $this->success('修改成功');
            }
            else{
                $this->error('修改失败');
            }
        }
        else{
            $data = CategoryModel::get(['id'=>$id,'type_id'=>$type_id]);
            $this->assign('data',$data);
            $this->assign('type_id',$type_id);
            $selectCategorys = CategoryService::getCategoryOptionsByTypeId($type_id,$data['parent_id']);
            $this->assign('selectCategorys',$selectCategorys);
            
            return $this->fetch();
        }
    }
    
    /**
     * 标签状态修改
     * @adminMenu(
     *     'name'   => '标签状态修改',
     *     'parent' => 'admin/Category/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签状态修改',
     *     'param'  => ''
     * )
     */
    public function status(){
        $id = $this->request->param('id',0,'intval');
        if(empty($id)){
            $this->error('参数错误');
        }
        $type = $this->request->param('type');
        $data[$type] = $this->request->param('value',0,'intval');
        $re = CategoryModel::update($data,['cid'=>$id],'status,sort');
        if($re){
            $this->success('修改成功'.$type);
        }
        else{
            $this->error('修改失败');
        }
    }
    
    /**
     * 标签删除
     * @adminMenu(
     *     'name'   => '标签删除',
     *     'parent' => 'admin/Category/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签删除',
     *     'param'  => ''
     * )
     */
    public function delete(){
        $ids = input('ids/a');
        if(!$ids){
            $this->error("请选择数据!");
        }
        $re = CategoryModel::destroy($ids);
        if($re){
            $this->success('删除成功!');
        }
        else{
            $this->error("删除失败!");
        }
        
    }
    
    /**
     * 标签关联删除
     * @adminMenu(
     *     'name'   => '标签关联删除',
     *     'parent' => 'admin/Category/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签关联删除',
     *     'param'  => ''
     * )
     */
    public function deletealias(){
        $id = input('id');
        $type_id = input('type_id');
        if(empty($id)||empty($type_id)){
            $this->error("请选择数据!");
        }
        $ids = CategoryService::getFullChilds($id, $type_id);
        $re = CategoryModel::destroy(['type_id'=>$type_id,'id'=>['in',$ids]]);
        if($re){
            $this->success('删除成功!');
        }
        else{
            $this->error("删除失败!");
        }
    
    }
}